import axios from 'axios';

const axiosInstance = axios.create({
    baseURL: 'https://api.saike.qqwj.live',
});

axiosInstance.interceptors.request.use(
    (config) => {
        const userInfo = JSON.parse(localStorage.getItem('userInfo'));
        if (userInfo !== null) {
            const token = userInfo.token;
            if (token) {
                config.headers.Authorization = `Bearer ${token}`;
            }
        }
        return config;
    },
    (error) => {
        return Promise.reject(error);
    }
);

export const HTTP = (url, data, method) => {
    return axiosInstance({
        method: method,
        url: url,
        data: method === 'POST' || method === 'PUT' ? data : null,
        params: method === 'GET' ? data : null,
    });
};

export default HTTP